2 // BaseNEncodingDecodingTest.m
5 // Copyright (c) 2020 Apple Inc. All rights reserved.
8 #import <XCTest/XCTest.h>
10 #include "mDNSEmbeddedAPI.h"
11 #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
13 @interface BaseNEncodingDecodingTest : XCTestCase
17 @implementation BaseNEncodingDecodingTest
19 char * encoded_str = NULL;
21 - (void) testBase64Encoding {
22 unsigned char data_input[1024];
25 char * test_cases[] = {
26 "", "f", "fo", "foo", "foob", "fooba", "foobar"
29 "", "Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy"
32 XCTAssertEqual(sizeof(test_cases), sizeof(answers));
34 for (int i = 0, limit = sizeof(answers) / sizeof(char *); i < limit; i++) {
35 test_case_ptr = test_cases[i];
36 answer_ptr = answers[i];
37 strlcpy((char *)data_input, test_case_ptr, sizeof(data_input));
39 XCTAssertEqual(strlen(answer_ptr), get_base_n_encoded_str_length(DNSSEC_BASE_64, strlen(test_case_ptr)));
41 encoded_str = base_n_encode(DNSSEC_BASE_64, data_input, strlen(test_case_ptr));
42 XCTAssertTrue(encoded_str != NULL);
43 XCTAssertTrue(strcmp(encoded_str, answer_ptr) == 0, "i: %d, input: %s, encoded_str: %s, answer_ptr: %s",
44 i, test_case_ptr, encoded_str, answer_ptr);
51 - (void) testBase32HexEncoding {
52 unsigned char data_input[1024];
55 char * test_cases[] = {
56 "", "f", "fo", "foo", "foob", "fooba", "foobar"
59 "", "CO======", "CPNG====", "CPNMU===", "CPNMUOG=", "CPNMUOJ1", "CPNMUOJ1E8======"
62 XCTAssertEqual(sizeof(test_cases), sizeof(answers));
64 for (int i = 0, limit = sizeof(answers) / sizeof(char *); i < limit; i++) {
65 test_case_ptr = test_cases[i];
66 answer_ptr = answers[i];
67 strlcpy((char *)data_input, test_case_ptr, sizeof(data_input));
69 XCTAssertEqual(strlen(answer_ptr), get_base_n_encoded_str_length(DNSSEC_BASE_32_HEX, strlen(test_case_ptr)));
71 encoded_str = base_n_encode(DNSSEC_BASE_32_HEX, data_input, strlen(test_case_ptr));
72 XCTAssertTrue(encoded_str != NULL);
73 XCTAssertTrue(strcmp(encoded_str, answer_ptr) == 0, "i: %d, input: %s, encoded_str: %s, answer_ptr: %s",
74 i, test_case_ptr, encoded_str, answer_ptr);
82 if (encoded_str != NULL) {
88 #endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)